<?php

class Emms_Form_Sponsor_Partner extends HTech_Form
{
    protected $_primary  = 'PartnerId';
            
    public function getValues()
    {
        $data = parent::getValues();
        if ($data['Password'] != '') $data['Password'] = md5($data['Password']);
        else unset($data['Password']);
        unset($data['Confirm']);
        return $data;
    }
    
    public function __construct() 
    {
        parent::__construct();        
        
        $this->setName('Partner');
        
        $this->setAttrib('title', $this->translate("Partner form"));
        $this->setDescription($this->translate("Edit partner info or add a new one"));
        
        $partnerId = new Zend_Form_Element_Hidden('PartnerId'); 
                
        $partner = new Zend_Form_Element_Text('Partner');
        $partner->setLabel("Partner")
            ->setRequired(true)
            ->addValidator('NotEmpty', true, array('messages' => $this->translate("Required field, can't be empty")))
            ->addFilter('Alpha', array('allowwhitespace' => true))
            ->addValidator('StringLength',false,
                array(
                    'min'      => 3,
                    'max'      => 255,
                    'messages' => $this->translate("From %min% to %max% characters only")
                ))
            ->addValidator('Db_NoRecordExists', true, array(
                'table' => 'partners', 
                'field' => 'Partner', 
                'exclude' => array('field' => 'PartnerId', 'value' => $this->_id),
                'messages' => $this->translate("Non valid or duplicated entry"))); 
        
        $username = new Zend_Form_Element_Text('Username');
        $username->setLabel("Username")
            ->setRequired(true)
            ->addFilter('Alpha')
            ->addFilter('StringToLower')            
            ->addValidator('NotEmpty', true, array('messages' => $this->translate("Required field, can't be empty")))
            ->addValidator('StringLength', true, array(
                'min' => 4, 
                'max' => 32, 
                'messages' => $this->translate("From %min% to %max% characters only")))
            ->addValidator('Db_NoRecordExists', true, array(
                'table' => 'partners', 
                'field' => 'Username', 
                'exclude' => array('field' => 'PartnerId', 'value' => $this->_id),
                'messages' => $this->translate("Non valid or duplicated entry")));
        
        $password = new Zend_Form_Element_Password('Password');
        $password->setLabel("Password")
            ->addValidator('StringLength', true, array(
                'min' => 4,
                'max' => 32,
                'messages' => $this->translate("From %min% to %max% characters only")));
        
        $confirm = new Zend_Form_Element_Password('Confirm');
        $confirm->setLabel("Confirm")
            ->addValidator('Identical', true, array(
                'token' => 'Password',
                'messages' => $this->translate("Password and Confirm do not match")));  
        
        $firstName = new Zend_Form_Element_Text('FirstName');
        $firstName->setLabel("First name")
            ->setRequired(true)
            ->addValidator('NotEmpty', true, array('messages' => $this->translate("Required field, can't be empty")))
            ->addFilter('Alpha')
            ->addValidator('StringLength', true, array(
                    'min' => 2,
                    'max' => 64,
                    'messages' => $this->translate("From %min% to %max% characters only")));
        
        $middleName = new Zend_Form_Element_Text('MiddleName');
        $middleName->setLabel("Middle name")
            ->setRequired(false)
            ->addFilter('Alpha')
            ->addValidator('StringLength', true, array(
                    'min' => 1,
                    'max' => 64,
                    'messages' => $this->translate("From %min% to %max% characters only")));
        
        $lastName = new Zend_Form_Element_Text('LastName');
        $lastName->setLabel("Last name")
            ->setRequired(true)
            ->addValidator('NotEmpty', true, array('messages' => $this->translate("Required field, can't be empty")))
            ->addFilter('Alpha')
            ->addValidator('StringLength', true, array(
                    'min' => 2,
                    'max' => 64,
                    'messages' => $this->translate("From %min% to %max% characters only")));

        $secondLastName = new Zend_Form_Element_Text('SecondLastName');
        $secondLastName->setLabel("Second last name")
            ->setRequired(false)
            ->addFilter('Alpha')
            ->addValidator('StringLength', false, array(
                    'min' => 1,
                    'max' => 64,
                    'messages' => $this->translate("From %min% to %max% characters only")));

        $email = new Zend_Form_Element_Text('Email');
        $email->setLabel("Email")
            ->setRequired(true)        
            ->addValidator('EmailAddress', true)
            ->addFilter('StringToLower')
            ->addFilter('StringTrim')
            ->addErrorMessage($this->translate("Non valid or duplicated email address"))
            ->addValidator('Db_NoRecordExists', true, array(
                'table' => 'partners', 
                'field' => 'Email', 
                'exclude' => array('field' => 'PartnerId', 'value' => $this->_id)));    
        
        $status = new Zend_Form_Element_Select('Status');
        $status->setLabel("Status")                
            ->setAttrib('size', '1')
            ->setRequired(true)
            ->addValidator('NotEmpty', true, array('messages' => $this->translate("Required field, can't be empty")))
            ->addMultiOptions(array('' => '--- ' . $this->translate('Select option'), 'Active' => $this->translate("Active"), 'Inactive' => $this->translate("Inactive")));   
                        
        $comments = new Zend_Form_Element_Textarea('Comments');
        $comments->setLabel("Comments")
            ->addFilter('Alnum', array('allowwhitespace' => true))
            ->setAttrib('rows', 2); 
        
        $submit = new Zend_Form_Element_Submit('Submit');
        $submit->setLabel("Submit")
            ->addDecorator(new HTech_Form_Decorator_Button(array('type' => 'submit')));        
        
        //$this->setDecorators(array(array('ViewScript', array('viewScript' => 'user/_form_add.phtml'))));
        
        $this->addElements(array($partner, $username, $password, $confirm, $firstName, $middleName, $lastName, $secondLastName,
            $status, $email, $comments, $submit, $partnerId));
    }

}